//
//  ZFooterView.m
//  danpin
//
//  Created by chuangjia on 18/9/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "ZFooterView.h"
#import "viewconfig.h"
#import "UILabel+StringFrame.h"
@interface ZFooterView()
{
    
}

@property(nonatomic,strong)UIButton *writeBtn;
@property(nonatomic,strong)UIButton *seeBtn;
@property(nonatomic,strong)UIButton *loveBtn;
@property(nonatomic,strong)UIButton *buyBtn;
@property(nonatomic,strong)UILabel *cmtLable;
@end


@implementation ZFooterView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setFrame:frame];
        [self initUIWithType:0];
    }
    return self;
}

-(void)setCallBack:(void(^)(FooterEventType,UIView *,id,id))callback
{
    self.actionBlock =callback;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
}
-(void)dealloc
{
    self.actionBlock=nil;
    self.writeBtn=nil;
    self.seeBtn=nil;
    self.loveBtn=nil;
    self.buyBtn=nil;
}



-(void)initUIWithType:(int)type
{
//    self.backgroundColor = [UIColor redColor];
//    UIView *bgView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
 //   bgView.backgroundColor= DARK_BACKGROUND_COLOR;//／／UIColorFromRGB(0x010101);
//    [self addSubview:bgView];
    
    //  背景色黑色半透明
    self.backgroundColor = DARK_BACKGROUND_COLOR;//[UIColor blackColor];
    
    CGFloat blockw =(DEVICE_WIDTH-widthAuto(100))/3;
    CGFloat xpost =(blockw-widthAuto(30))/2;
    _writeBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    _writeBtn.frame =CGRectMake(xpost-widthAuto(30), (BOTTOM_TOOLBAR_HEIGHT- hightAuto(30))/2, widthAuto(80), hightAuto(30));
    _writeBtn.backgroundColor=[UIColor clearColor];
    _writeBtn.tag=0;
    _writeBtn.titleLabel.font = FONT(12);
    [_writeBtn setImage:[UIImage imageNamed:@"toolbar_vote_edit.png"] forState:UIControlStateNormal];
    [_writeBtn setTitleColor:UIColorFromRGB(0x898989) forState:UIControlStateNormal];
    [_writeBtn setTitle:@"评论" forState:UIControlStateNormal];
    [_writeBtn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_writeBtn];
    
    UIView *lineview =[[UIView alloc] initWithFrame:CGRectMake(blockw, (BOTTOM_TOOLBAR_HEIGHT- hightAuto(20))/2, 0.5,hightAuto(20))];
   lineview.backgroundColor=UIColorFromRGB(0x898989);
   [self addSubview:lineview];
   lineview=nil;
    
    _seeBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    _seeBtn.tag=1;
    _seeBtn.frame =CGRectMake(blockw+xpost, (BOTTOM_TOOLBAR_HEIGHT- hightAuto(30))/2, widthAuto(30), hightAuto(30));
    _seeBtn.backgroundColor=[UIColor clearColor];
    [_seeBtn setImage:[UIImage imageNamed:@"toolbar_vote.png"] forState:UIControlStateNormal];
    [_seeBtn setImage:[UIImage imageNamed:@"toolbar_vote.png"] forState:UIControlStateSelected];
    [_seeBtn setImage:[UIImage imageNamed:@"toolbar_vote.png"] forState:UIControlStateHighlighted];
    [_seeBtn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_seeBtn];
    
    _cmtLable = [[UILabel alloc] initWithFrame:CGRectZero];
    _cmtLable.backgroundColor=UIColorFromRGB(0xCE0202);
    _cmtLable.textColor =[UIColor whiteColor];
    _cmtLable.textAlignment=NSTextAlignmentCenter;
    _cmtLable.layer.cornerRadius=3;
    _cmtLable.layer.masksToBounds=YES;
    _cmtLable.font=FONT(11);
    _cmtLable.hidden=YES;
    [self addSubview:_cmtLable];
    
    _loveBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    _loveBtn.tag=2;
    _loveBtn.frame =CGRectMake(blockw*2+xpost, (BOTTOM_TOOLBAR_HEIGHT- hightAuto(30))/2, widthAuto(30), hightAuto(30));
    _loveBtn.backgroundColor=[UIColor clearColor];
    [_loveBtn setImage:[UIImage imageNamed:@"toolbar_like.png"] forState:UIControlStateNormal];
    [_loveBtn setImage:[UIImage imageNamed:@"toolbar_like.png"] forState:UIControlStateSelected];
    [_loveBtn setImage:[UIImage imageNamed:@"toolbar_like.png"] forState:UIControlStateHighlighted];
    [_loveBtn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_loveBtn];
    
    _buyBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    _buyBtn.tag=3;
    _buyBtn.frame =CGRectMake(DEVICE_WIDTH-widthAuto(100), 0, widthAuto(100), BOTTOM_TOOLBAR_HEIGHT);
    _buyBtn.backgroundColor= BLUE_COLOR;//UIColorFromRGB(0xCE0202);
    _buyBtn.titleLabel.font =FONT(15);
    [_buyBtn setTitle:@"去购买" forState:UIControlStateNormal];
    _buyBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
    [_buyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_buyBtn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_buyBtn];
//    bgView=nil;
    
}
-(void)clickEvent:(id)sender
{
    UIButton*btn =(UIButton*)sender;
    FooterEventType type=FooterEventType_unknow;
    switch (btn.tag) {
        case 0:
        {
            type =FooterEventType_voteedit;
        }
            break;
        case 1:
        {
            type =FooterEventType_vote;
        }
            break;
        case 2:
        {
            type =FooterEventType_love;
        }
            break;
        case 3:
        {
            type =FooterEventType_buy;
        }
            break;
        default:
            break;
    }
    if (self.actionBlock) {
        self.actionBlock(type,self,nil,nil);
    }
}
-(void)setIsLove:(BOOL)isLove
{
    _isLove=isLove;
    if (_isLove) {
        [_loveBtn setImage:[UIImage imageNamed:@"toolbar_like_selected.png"] forState:UIControlStateNormal];
    }else{
        [_loveBtn setImage:[UIImage imageNamed:@"toolbar_like.png"] forState:UIControlStateNormal];
    }
}
-(void)setCommentCount:(NSString *)commentCount
{
    if (commentCount==nil || [commentCount length]==0) {
        _cmtLable.hidden=YES;
    }else{
        _cmtLable.hidden=NO;
        CGFloat blockw =(DEVICE_WIDTH-widthAuto(100))/3;
        CGFloat xpost =(blockw-widthAuto(30))/2;
        
        _cmtLable.text=commentCount;
        CGSize size =[_cmtLable boundingRectWithSize:CGSizeMake(100, 14)];
        _cmtLable.frame =CGRectMake(blockw*2-xpost-size.width-4, (BOTTOM_TOOLBAR_HEIGHT- hightAuto(30))/2, size.width+4, 14);
    }
}
-(void)setPrice:(NSString *)price
{
    if (price==nil || [price length]==0 ||[price intValue]==-1) {
        _buyBtn.backgroundColor = [UIColor lightGrayColor];
        _buyBtn.enabled=NO;
        UILabel *bLable =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, _buyBtn.frame.size.width, _buyBtn.frame.size.height)];
        bLable.backgroundColor =[UIColor clearColor];
        bLable.textColor =UIColorFromRGB(0xf2f2f2);
        bLable.textAlignment=NSTextAlignmentCenter;
        bLable.font=FONT(15);
        bLable.text =@"购买";
        [_buyBtn addSubview:bLable];
        bLable=nil;
    }else if (price!=nil && [price length]>0 && [price isEqualToString:@"1"])
    {
        _buyBtn.backgroundColor = UIColorFromRGB(0xCE0202);
        _buyBtn.enabled=YES;
        
        UILabel *bLable =[[UILabel alloc] initWithFrame:CGRectMake(0, 3, _buyBtn.frame.size.width, _buyBtn.frame.size.height)];
        bLable.backgroundColor =[UIColor clearColor];
        bLable.textColor =[UIColor whiteColor];
        bLable.textAlignment=NSTextAlignmentCenter;
        bLable.font=FONT(15);
        bLable.text =@"购买";
        [_buyBtn addSubview:bLable];
        bLable=nil;
        
    }else
    {
        _buyBtn.backgroundColor = UIColorFromRGB(0xCE0202);
        _buyBtn.enabled=YES;
        
        UILabel *bLable =[[UILabel alloc] initWithFrame:CGRectMake(0, 3, _buyBtn.frame.size.width, _buyBtn.frame.size.height/2)];
        bLable.backgroundColor =[UIColor clearColor];
        bLable.textColor =[UIColor whiteColor];
        bLable.textAlignment=NSTextAlignmentCenter;
        bLable.font=FONT(15);
        bLable.text =@"购买";
        [_buyBtn addSubview:bLable];
        bLable=nil;
        
        UILabel *pLable =[[UILabel alloc] initWithFrame:CGRectMake(0, _buyBtn.frame.size.height/2, _buyBtn.frame.size.width, _buyBtn.frame.size.height/2)];
        pLable.backgroundColor =[UIColor clearColor];
        pLable.textColor =[UIColor whiteColor];
        pLable.textAlignment=NSTextAlignmentCenter;
        pLable.font=FONT(13);
        pLable.text =price;
        [_buyBtn addSubview:pLable];
        pLable=nil;
        
        
    }
}
@end
